package models

import (
	"cloud-navigation/global"
	"errors"

	"gorm.io/gorm"
)

type Admin struct {
	Models
	Name      string `json:"name" gorm:"not null;unique;comment:用户名称"`
	Mobile    string `json:"mobile" gorm:"not null;unique;comment:电话号码"`
	Password  string `json:"password" gorm:"not null;comment:管理员密码"`
	AdminLogs []AdminLogs
}

type AdminSafe struct {
	Models
	Name   string `json:"name" gorm:"not null;unique;comment:用户名称"`
	Mobile string `json:"mobile" gorm:"not null;unique;comment:电话号码"`
}

func (admin *Admin) BeforeDelete(tx *gorm.DB) (err error) {
	var count int64
	global.App.DB.Model(&Admin{}).Count(&count)

	if count == 1 {
		return errors.New("必须保留至少一个管理员")
	}
	return
}
